<template>
  <div class="addTechnicalSecrets-container-padding">
    <div v-if="!disabled" class="buttonList">
      <el-button type="primary" @click="handleSaveDraft">保存为草稿</el-button>
      <el-button type="primary" @click="submit">提交</el-button>
    </div>
    <el-form
      ref="baseInfoRef"
      :model="formline"
      :rules="rules"
      class="patent-form"
      label-width="auto"
    >
      <el-form-item label="技术秘密名称:" prop="technologySecretName">
        <el-input maxlength="30" v-model="formline.technologySecretName" />
      </el-form-item>
      <el-form-item label="发明人:">
        <div class="people-list">
          <div
            v-for="(item, index) in formline.inventors"
            :key="index"
            class="people"
          >
            <el-input maxlength="30" v-model="item.name" placeholder="姓名" />
            <el-input
              maxlength="30"
              v-model="item.mobile"
              placeholder="手机号"
            />
            <el-input
              maxlength="30"
              v-model="item.employeeCode"
              placeholder="员工编号"
            />
            <el-input
              maxlength="30"
              v-model="item.contributeProportion"
              placeholder="贡献率 %"
            />
            <el-button
              v-if="index !== 0"
              type="danger"
              :icon="Delete"
              circle
              @click="removeInventor(index)"
            />
          </div>
          <div class="last">
            <el-button type="primary" text :icon="Plus" @click="addInventor">
              增加发明人
            </el-button>
          </div>
        </div>
        <!-- <el-form
          :inline="true"
          :model="formline"
          :rules="rules"
          class="patent-form"
          label-width="auto"
        >
          <el-form-item label="姓名" prop="patentee">
            <el-input v-model="formline.patentee" />
          </el-form-item>
          <el-form-item label="手机号" prop="patentee">
            <el-input v-model="formline.patentee" />
          </el-form-item>
          <el-form-item label="员工编号" prop="patentee">
            <el-input v-model="formline.patentee" />
          </el-form-item>
          <el-form-item label="贡献率 %" prop="patentee">
            <el-input v-model="formline.patentee" />
          </el-form-item>
        </el-form> -->
      </el-form-item>
      <el-form-item label="完成单位:" prop="completeUnit">
        <el-input maxlength="30" v-model="formline.completeUnit" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
  import { reactive, onMounted, ref } from 'vue'
  import { Delete, Plus } from '@element-plus/icons-vue'
  import { postDraftBoxCreate } from '@/api/project/drafts'
  import {
    updateTechnical,
    getTechnicalInfo,
    addTechnicalSecrets,
  } from '@/api/property'
  import { useCloseTabNavigate } from '@/utils/useCloseTabNavigate'
  import { useRoute } from 'vue-router'
  const props = withDefaults(
    defineProps<{
      disabled?: boolean
    }>(),
    {
      disabled: false,
    }
  )

  const rules = reactive({})
  const route = useRoute()
  const { closeTabNavigate } = useCloseTabNavigate()

  const formline = reactive({
    technologySecretName: '',
    completeUnit: '',
    inventors: [
      { name: '', mobile: '', employeeCode: '', contributeProportion: '' },
    ],
  })

  const technicalId = ref('')

  onMounted(() => {
    const id = (route.query.id as string) ?? ''
    technicalId.value = id
    if (id) {
      getTechnicalInfo({ id: id, getType: 2 }).then((res) => {
        for (const key in res.data) {
          // if (key === 'inventors') {

          // }
          formline[key] = res.data[key]
        }
      })
    }
    try {
      const inputList = document.querySelectorAll('input.el-input__inner')
      const maxLength = 30
      inputList.forEach((input: any) => {
        input.maxLength = maxLength
      })
    } catch (error) {}
  })

  const addInventor = () => {
    formline.inventors.push({
      name: '',
      mobile: '',
      employeeCode: '',
      contributeProportion: '',
    })
  }
  const removeInventor = (index: number) => {
    formline.inventors.splice(index, 1)
  }

  // 草稿
  const handleSaveDraft = async () => {
    try {
      const params = {
        id: route.query.id || '',
        type: 'TECH_SECRET',
        submitJson: JSON.stringify({
          ...formline,
          draftBoxId: route.query?.id || '',
        }),
      }
      const res: any = await postDraftBoxCreate(params)
      if (res && res.code === 0) {
        ElMessage.success('保存草稿成功')
        closeTabNavigate({ name: 'TechnicalSecrets' })
      } else {
        ElMessage.error(`保存草稿失败: ${res.msg || '未知错误'}`)
      }
    } catch (e) {
      console.error('保存草稿失败', e)
    }
  }
  const submit = () => {
    if (formline.inventors[0].name == '') {
      ElMessage.warning('发明人不能为空')
      return
    }
    if (technicalId.value) {
      let params = {
        id: technicalId.value,
        ...formline,
      }
      updateTechnical(params).then((res) => {
        ElMessage.warning('修改成功')
        closeTabNavigate({ name: 'TechnicalSecrets' })
      })
    } else {
      let params = {
        ...formline,
      }
      addTechnicalSecrets(params).then((res) => {
        ElMessage.warning('增加成功')
        closeTabNavigate({ name: 'TechnicalSecrets' })
      })
    }
  }
</script>

<style scoped lang="scss">
  $margin-bottom: 20px;

  .buttonList {
    display: flex;
    justify-content: end;
    padding-bottom: $margin-bottom;
    border-bottom: 1px solid #edeff2;
    margin-bottom: $margin-bottom;
  }

  .patent-form {
    width: 60%;
    // margin: 0 auto;
  }

  .people {
    display: grid;
    grid-template-columns: repeat(4, 1fr) 0.5fr;
    grid-column-gap: 20px;
  }

  .people + .people {
    margin-top: $margin-bottom;
  }

  .last {
    margin-top: $margin-bottom;
  }
</style>
